package com.hskn.hss.module.tlcostdateil.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.eventtoremind.mapper.EventToRemindMapper;
import com.hskn.hss.module.storageDetail.entity.TlStorageDetail;
import com.hskn.hss.module.tlcostdateil.entity.TlCostDateil;
import com.hskn.hss.module.tlcostdateil.mapper.TlCostDateilMapper;
import com.hskn.hss.module.tlcostdateil.service.ITlCostDateilService;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-05
 */
@Slf4j
@RestController
@RequestMapping("/hss/tlcostdateil")
public class TlCostDateilController {

    @Autowired
    private ITlCostDateilService TlCostDateilService;

    /***
     * 新增信息
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.INSERT, tableName = "tl_cost_dateil", bean = TlCostDateil.class, beanMapper = TlCostDateilMapper.class, names = "id")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public AjaxResult<Object> insert(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        return TlCostDateilService.insert(params);
    }

    /**
     * 修改信息
     *
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.UPDATE, tableName = "tl_cost_dateil", idKey = "id", bean = TlCostDateil.class, beanMapper = TlCostDateilMapper.class, names = "id")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        return TlCostDateilService.update(params);
    }

    /***
     * 删除角色信息
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.DELETE, tableName = "tl_cost_dateil", idKey = "id", bean = TlCostDateil.class, beanMapper = TlCostDateilMapper.class, names = "id")
    @RequestMapping(value = "deleteByid", method = RequestMethod.POST)
    public AjaxResult<Object> deleteByid(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        return TlCostDateilService.deleteByid(params.getString("id"));
    }

    /**
     * 根据Id获取信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getitemByid", method = RequestMethod.POST)
    public AjaxResult<Object> getitemByid(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        TlCostDateil tlCostDateil = null;
        try {
            tlCostDateil = TlCostDateilService.getById(params.getString("id"));
            return AjaxResult.success(tlCostDateil);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }


    }

    /**
     * 根据menuid获取列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getitemListByParentId", method = RequestMethod.POST)
    public AjaxResult<Object> getitemListByParentId(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            List<TlCostDateil> tlCostDateilList = TlCostDateilService.getitemListByParentId(params.getString("maintbid"));
            return AjaxResult.success(tlCostDateilList);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }

    }
}

